//
//  SSNetworkManager.m
//  starspaceVR
//
//  Created by 曹端芹 on 17/9/12.
//  Copyright © 2017年 starspace. All rights reserved.
//

#import "SSNetworkManager.h"

static NSString *const httpBaseURL = @"http://api.vrspaces.cn/api/";

@implementation SSNetworkManager

+ (AFHTTPSessionManager *)httpsPostManager {

    static AFHTTPSessionManager *httpsPostManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        httpsPostManager = [[AFHTTPSessionManager alloc] initWithBaseURL:nil];
        httpsPostManager.responseSerializer = [AFHTTPResponseSerializer serializer];
        //https证书
        AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey];
        securityPolicy.allowInvalidCertificates = YES;
        httpsPostManager.securityPolicy = securityPolicy;
    });
    return httpsPostManager;
}

+ (AFHTTPSessionManager *)httpPostManager {
    static AFHTTPSessionManager *httpPostManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        AFHTTPSessionManager *httpManager  = [[AFHTTPSessionManager alloc] initWithBaseURL:nil];
        
        httpManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
        httpManager.requestSerializer.timeoutInterval = 10.0;
    });
    
    return httpPostManager;
}

+ (AFHTTPSessionManager *)httpGetManager {

    static AFHTTPSessionManager *httpGetManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        httpGetManager = [[AFHTTPSessionManager alloc]initWithBaseURL:nil];
        httpGetManager.responseSerializer = [AFJSONResponseSerializer serializer];
        httpGetManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"text/html",@"application/javascript",@"application/json", nil];
        httpGetManager.requestSerializer = [AFJSONRequestSerializer serializer];
        httpGetManager.requestSerializer.timeoutInterval = 10.0;
    
    });
    return httpGetManager;
}


/*
 *  HTTPS 请求
 */
+ (NSURLSessionDataTask *)postHTTPSWithURLString:(NSString *)URLString parameters:(NSDictionary *)parameters success:(RequestSuccess)success failure:(RequestFailure)failure {

    NSURLSessionDataTask *dataTask = [self postWithMethod:RequestMethodHTTPS URLString:URLString parameters:parameters success:success failure:failure];
    return dataTask;
}

/*
 * HTTP
 */

+ (NSURLSessionDataTask *)postWithAPIString:(NSString *)APIString parameters:(NSDictionary *)parameters success:(RequestSuccess)success failure:(RequestFailure)failure {

    NSURLSessionDataTask *dataTask = [self postWithMethod:RequestMethodHTTP URLString:APIString parameters:parameters success:success failure:failure];
    return dataTask;
}

/*
 * 上传图片
 */
+ (NSURLSessionDataTask *)uploadFilesWithURLString:(NSString *)URLString parameters:(NSDictionary *)parameters constructingBodyWithBlock:(void (^)(id<AFMultipartFormData>))block success:(RequestSuccess)success failure:(RequestFailure)failure {

    AFHTTPSessionManager *manager = [self httpPostManager];
//    NSString *url = httpBaseURL;
    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:parameters];
    NSString *sign = @"1426de94585c43c50c7239905a7da4a8";
    param[@"sign"] = sign;
    
    NSURLSessionDataTask *dataTask = [manager POST:URLString parameters:param constructingBodyWithBlock:block progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    return dataTask;
}

/*
 * HTTP GET请求
 */

+ (NSURLSessionDataTask *)getWithURLString:(NSString *)URLString parameters:(NSDictionary *)parameters success:(RequestSuccess)success failure:(RequestFailure)failure {
    
    AFHTTPSessionManager *manger = [self httpGetManager];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:parameters];
    NSString *sign = @"1426de94585c43c50c7239905a7da4a8";
    param[@"sign"] = sign;
    
    NSString *url = [NSString stringWithFormat:@"%@%@",httpBaseURL,URLString];

    NSURLSessionDataTask *dataTask = [manger GET:url parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    return dataTask;

}

+ (NSURLSessionDataTask *)postWithMethod:(RequestMethod)requestMethod URLString:(NSString *)URLString parameters:(NSDictionary *)parameters success:(RequestSuccess)success failure:(RequestFailure)failure {
    
    AFHTTPSessionManager *manager = nil;
    NSString *url;
    if (requestMethod == RequestMethodHTTP) {
        manager = [self httpPostManager];
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:parameters];
    
    
    NSURLSessionDataTask *task = [manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
            
        }
    }];

    return task;

}

@end
